from PyQt5.QtWidgets import QToolBar, QAction, QSpinBox, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSignal

class Toolbar(QToolBar):
    # 添加信号
    file_open_triggered = pyqtSignal()
    file_save_triggered = pyqtSignal()
    fit_page_triggered = pyqtSignal()
    fit_width_triggered = pyqtSignal()
    page_changed = pyqtSignal(int)  # 添加页码改变信号
    
    def __init__(self):
        super().__init__()
        self.init_ui()
        self.connect_signals()
    
    def init_ui(self):
        self.open_action = QAction("打开", self)
        self.save_action = QAction("保存", self)
        self.addAction(self.open_action)
        self.addAction(self.save_action)
        
        self.prev_page_action = QAction("上一页", self)
        self.next_page_action = QAction("下一页", self)
        
        self.addSeparator()
        self.addAction(self.prev_page_action)
        self.addAction(self.next_page_action)
        
        self.addSeparator()
        
        self.fit_page_action = QAction("适合页面", self)
        self.fit_width_action = QAction("适合宽度", self)
        
        self.addAction(self.fit_page_action)
        self.addAction(self.fit_width_action)
        
        self.addSeparator()
        
        # 添加页码输入框
        self.page_spinbox = QSpinBox(self)
        self.page_spinbox.setMinimum(1)  # 默认设置为1，避免显示为"-"
        self.page_spinbox.setValue(1)
        self.page_spinbox.setEnabled(False)
        self.addWidget(self.page_spinbox)
        
        # 添加总页数标签
        self.total_pages_label = QLabel("/ 0", self)
        self.addWidget(self.total_pages_label)
    
    def connect_signals(self):
        self.open_action.triggered.connect(self.file_open_triggered.emit)
        self.save_action.triggered.connect(self.file_save_triggered.emit)
        self.fit_page_action.triggered.connect(self.fit_page_triggered.emit)
        self.fit_width_action.triggered.connect(self.fit_width_triggered.emit)
        self.page_spinbox.valueChanged.connect(self.page_changed.emit)

    def update_page_number(self, page_num):
        """更新页码显示，但不触发信号
        
        Args:
            page_num: 当前页码(从1开始)
        """
        # 暂时阻断信号连接，避免循环触发
        self.page_spinbox.blockSignals(True)
        if self.page_spinbox.isEnabled() and page_num >= self.page_spinbox.minimum():
            self.page_spinbox.setValue(page_num)
        self.page_spinbox.blockSignals(False)

    def reset_page_controls(self):
        """重置页面导航控件"""
        self.enable_page_controls(False)

    def enable_page_controls(self, enabled=True, total_pages=None):
        """启用或禁用页面导航按钮
        
        Args:
            enabled (bool): 是否启用页码控制
            total_pages (int, optional): 总页数
        """
        self.prev_page_action.setEnabled(enabled)
        self.next_page_action.setEnabled(enabled)
        
        if enabled and total_pages is not None:
            self.page_spinbox.blockSignals(True)  # 阻止信号以避免不必要的触发
            self.page_spinbox.setMinimum(1)  # 确保最小页码为1
            self.page_spinbox.setMaximum(total_pages)
            self.page_spinbox.setValue(1)  # 明确设置第一页
            self.total_pages_label.setText(f"/ {total_pages}")
            self.page_spinbox.setEnabled(True)  # 先设置值，再启用控件
            self.page_spinbox.blockSignals(False)  # 恢复信号连接
        else:
            self.page_spinbox.blockSignals(True)
            self.page_spinbox.setEnabled(False)
            self.page_spinbox.setMinimum(1)  # 保持最小值为1
            self.page_spinbox.setValue(1)  # 即使禁用也保持值为1
            self.total_pages_label.setText("/ 0")
            self.page_spinbox.blockSignals(False)
